package com.vanzo.encryption;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;

import com.vanzo.util.LogUtil;

/**
 * Created by tisen on 2018/2/6.
 */

public class TestActivity extends AppCompatActivity {

    private TextView console;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_encryption_test);
        console = (TextView) findViewById(R.id.encryption_console);
        Encryption.NFC nfc1;
        byte[] gk = Encryption.getGroupKey();
        addConsole("gk = " + ByteUtil.byteArrayToHexStr(gk));
        addConsole("gk length = " + gk.length);
        LogUtil.put(this, "gk = " + ByteUtil.byteArrayToHexStr(gk), LogUtil.WARN);
        LogUtil.put(this, "gk length = " + gk.length, LogUtil.WARN);
        byte[] kk;
        while (true) {
            kk = Encryption.getKillKey();
            LogUtil.put(this, "kk = " + ByteUtil.byteArrayToHexStr(kk), LogUtil.WARN);
            LogUtil.put(this, "kk length = " + kk.length, LogUtil.WARN);
            byte[] nfc = Encryption.getNfc2(1, 2, 1, 5, 1, 0, gk, kk);
            LogUtil.put(this, "nfc length = " + nfc.length, LogUtil.WARN);
            if (nfc.length <= 3000) {
                addConsole("kk = " + ByteUtil.byteArrayToHexStr(kk));
                addConsole("nfc = " + ByteUtil.byteArrayToHexStr(nfc));
                addConsole("nfc length = " + nfc.length);


                nfc1 = Encryption.parseNFCFromByte(nfc, nfc.length);
                break;
            }
        }

        addConsole("nfc.binary = " + ByteUtil.byteArrayToHexStr(nfc1.getBinary()));
        addConsole("nfc.binary length = " + nfc1.getLength());
        addConsole("binary = " + nfc1.toString());


        byte[] deviceStatus = Encryption.getDeviceStatus();

        addConsole("deviceStatus = " + ByteUtil.byteArrayToHexStr(deviceStatus));
        addConsole("deviceStatus length = " + deviceStatus.length);

        byte[] lasCmdResult = Encryption.getLastCmdResult();

        addConsole("lasCmdResult = " + ByteUtil.byteArrayToHexStr(lasCmdResult));
        addConsole("lasCmdResult length = " + lasCmdResult.length);


        byte[] killMessage = Encryption.sendKillMessage(1, 1, kk);

        addConsole("killMessage = " + ByteUtil.byteArrayToHexStr(killMessage));
        addConsole("killMessage length = " + killMessage.length);

        byte[] msg = ByteUtil.strToByteArray("12345");

        byte[] message = Encryption.sendMessage(1, 1, msg);

        addConsole("message = " + ByteUtil.byteArrayToHexStr(message));
        addConsole("message length = " + message.length);
    }

    private void addConsole(String log) {
        if (log.length() > 100) {
            log = log.substring(0, 100);
        }
        console.append(log + "\r\n");
    }
}
